import { _decorator, Component, Node, ScrollBar, ScrollView, UITransform, Label, Button, Color, ProgressBarComponent, Sprite, SpriteFrame, Texture2D, Game } from 'cc';
import { EventMgr } from '../../../Framework/Scripts/Managers/EventMgr';
import { poolManager } from '../../../Framework/Scripts/Managers/poolManager';
import { ResMgr } from '../../../Framework/Scripts/Managers/ResMgr';
import { SoundMgr } from '../../../Framework/Scripts/Managers/SoundMgr';
import { UIComponent, UIMgr } from '../../../Framework/Scripts/Managers/UIMgr';
import { constant, GoldType } from '../constant';
import { GameApp } from '../GameApp';
import { UserData } from '../userData/UserData';
const { ccclass, property } = _decorator;


/** 
  周无邪出品
  本源码用于学习研究使用。
  作者联系方式：tuya7078（微信）。
 */
@ccclass('shop_player_UICtrl')
export class shop_player_UICtrl extends UIComponent {
    private scrollView: ScrollView = null
    private playerConfig: any = {}
    private UIItems = null

    private coinNum: Label = null


    upItem(btn, gameLevel, key, root, data) {
        EventMgr.Instance.Emit(constant.EVENT_NAME.upHomeTop, null)

        this.upCoin("", null)
        const item = this.UIItems[key]
        root.getChildByName("ProgressBar").getChildByName("level").getComponent(Label).string = item["level"] + "/" + item["max"]

        root.getChildByName("ProgressBar").getComponent(ProgressBarComponent).progress = item.level / item.max
        if (gameLevel < item.minLevel) {

            root.getChildByName("nullLabel").active = true
            //未开启技能
            btn.active = false
           
            root.getChildByName("nullLabel").getComponent(Label).string = "第" + item.minLevel + "关解锁"

        } else {


            if (item["level"] < item["max"]) {


                btn.active = true
             

                btn.getChildByName("gold").getComponent(Label).string = item["gold"]


    
                btn.on("click", () => {
                    if (UserData.Instance.localData["gold"] >= item["gold"]) {
                        btn.off("click")
                        SoundMgr.Instance.playSound(ResMgr.Instance.getAsset("Sounds", "levelUp"))
                        UserData.Instance.localData["playerConfig"][data["playerType"]]["skill"][key] += 1
                        UserData.Instance.localData["gold"] -= item["gold"]
                        UserData.Instance.saveLocalData()
                        this.upItem(btn, gameLevel, key, root, data)
                    } else {
                        GameApp.Instance.show_tip("金币不足")
                    
                    }




                }


                )


       
            } else {
                //     //隐藏购买按钮
                btn.active = false
              
                root.getChildByName("nullLabel").getComponent(Label).string = "已满级"
                root.getChildByName("nullLabel").active = true
            }
        }

    }

    init() {

        const data = UserData.Instance.getLocalData()//大表
        this.playerConfig = data["playerConfig"][data["playerType"]]
        //   console.log(this.playerConfig);
        this.UIItems = constant.characterConfig[data["playerType"]]["config"]
        for (let key in this.UIItems) {
            this.UIItems[key]["level"] = this.playerConfig["skill"][key]

        }







        //console.log(this.UIItems);
        //700 200
        let height: number = 0
        let posY: number = 250

        //用遍历删除节点池
        this.scrollView.content.destroyAllChildren()
        for (let key in this.UIItems) {
            const item = this.UIItems[key]
            const n: Node = poolManager.instance.getNode(ResMgr.Instance.getAsset("GUI", "item/shopItem"), this.scrollView.content)

            posY -= 250
            height += 250
            n.setPosition(0, posY, 0)

            this.scrollView.content.getComponent(UITransform).height = height
            const root: Node = n.getChildByName("bg")
            const btn: Node = root.getChildByName("btn")
            const adBtn: Node = root.getChildByName("adBtn")


            const imageAsset = ResMgr.Instance.getAsset("GUI", "skillIcon/" + key)

            const spriteFrame = new SpriteFrame();
            const texture = new Texture2D();
            texture.image = imageAsset;
            spriteFrame.texture = texture;



            root.getChildByName("iconBg").getChildByName("icon").getComponent(Sprite).spriteFrame = spriteFrame;

            root.getChildByName("iconBg").getChildByName("icon").getComponent(Sprite).trim = true


            root.getChildByName("name").getComponent(Label).string = item["name"]
            root.getChildByName("str").getComponent(Label).string = item["str"]
            btn.getChildByName("gold").getComponent(Label).string = item["gold"]

            root.getChildByName("nullLabel").active = false



            const gameLevel: number = UserData.Instance.localData["level"]


            this.upItem(btn, gameLevel, key, root, data)







        }

        this.scrollView.content.getComponent(UITransform).height = height
    }
    upCoin(name: string, data) {
        this.coinNum.string = UserData.Instance.localData["gold"]
    }
    onDestroy() {
        EventMgr.Instance.RemoveEventListener(constant.EVENT_NAME.upShopPlayerTop, this.upCoin, this)

    }
    onLoad() {
        EventMgr.Instance.AddEventListener(constant.EVENT_NAME.upShopPlayerTop, this.upCoin, this)
        this.coinNum = this.ViewNode("cion/Label").getComponent(Label)

        this.scrollView = this.ViewNode("ScrollView").getComponent(ScrollView)
        this.AddButtonListener("outBtn", this, () => {
            GameApp.Instance.isBack = false
            UIMgr.Instance.DestroyUIView(this.node.name)
        })

        this.init()


    }

    start() {

    }

    update(dt: number) {

    }
}


